package leetcode;

import java.util.Stack;

public class BasicCalculator {

	public static void main(String[] args) {
		BasicCalculator object = new BasicCalculator();
//		String string = "2-4-(8+2-6+(8+4-(1)+8-10))";
//		System.out.println(object.calculate(string));
		
		String s = "5 - 3 * 2 + 15 / 3 + 2";
		System.out.println(object.calculateII(s));
	}
	
	//Runtime 283ms, beat 1.55%
	public int calculate(String s) {
        if(s == null || s.length() <= 0){
        	return 0;
        }
        int length = s.length();
        int a = 0, b = 0;
        int operator = '+';
        for(int i = 0; i < length; i++){
        	char c = s.charAt(i);
        	//遇到括号我们就首先进行递归
        	if(c == '('){
        		int j = i;
        		int count = 1;
        		while(i < length){
        			i++;
        			if(s.charAt(i) == '('){
        				System.out.println(" i :" + i);
        				count++;
        			}
        			if(s.charAt(i) == ')'){
        				if(count == 1){
        					break;
        				}else{
        					count--;
        				}
        			}
        		}
        		b = calculate(s.substring(j + 1, i));
        		System.out.println("j : " + j + " i: " + i + " b: " + b);
        	}else if(c == '+' || c == '-'){
        		switch (operator) {
				case '+':
					a += b;
					break;
				case '-':
					a -= b;
					break;
				}
        		b = 0;
        		operator = c;
        		// System.out.println("cur a :" + a);
        	}else if(Character.isDigit(c)){
        		b = b * 10 + c - '0';
        		// System.out.println("c: " + c + " b: " + b);
        	}
        }
        switch (operator) {
		case '+':
			a += b;
			break;
		case '-':
			a -= b;
			break;
		}
        return a;
    }
	
	// Runtime: 17 ms, beat 70.12%
	public int calculate_2(String s) {
		Stack<Integer> stack = new Stack<Integer>();
		int result = 0;
		int number = 0;
		int sign = 1;
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			if (Character.isDigit(c)) {
				number = 10 * number + (int) (c - '0');
			} else if (c == '+') {
				result += sign * number;
				number = 0;
				sign = 1;
			} else if (c == '-') {
				result += sign * number;
				number = 0;
				sign = -1;
			} else if (c == '(') {
				// we push the result first, then sign;
				stack.push(result);
				stack.push(sign);
				// reset the sign and result for the value in the parenthesis
				sign = 1;
				result = 0;
			} else if (c == ')') {
				result += sign * number;
				number = 0;
				result *= stack.pop(); // stack.pop() is the sign before the
										// parenthesis
				result += stack.pop(); // stack.pop() now is the result
										// calculated before the parenthesis

			}
		}
		if (number != 0)
			result += sign * number;
		return result;
	}
	
	
	//含有+ - * / 四则运算
	public int calculateII(String s) {
        if(s == null || s.length() <= 0){
        	return 0;
        }
        int length = s.length();
        int number = 0;
        int number2 = 0;
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < length; i++){
        	char c = s.charAt(i);
			if (Character.isDigit(c)) {
				number = 10 * number + (int) (c - '0');
			} else if (c == '+' || c == '-') {
				sb.append(number).append(c);
				number = 0;
			} else if (c == '*') {
				i++;
				while(i < length && (s.charAt(i) == ' ' || Character.isDigit(s.charAt(i))) ){
					if(Character.isDigit(s.charAt(i))){
						number2 = number2 * 10 + s.charAt(i) - '0';
					}
					i++;
				}
				System.out.println(" number2 * " + number2 + " number: " + number);
				number = number * number2;
				number2 = 0;
				i--;
			} else if (c == '/') {
				i++;
				while(i < length && (s.charAt(i) == ' ' || Character.isDigit(s.charAt(i))) ){
					if(Character.isDigit(s.charAt(i))){
						number2 = number2 * 10 + s.charAt(i) - '0';
					}
					i++;
				}
				System.out.println(" number2 * " + number2 + " number: " + number);
				// sb.append(number / number2);
				number /= number2;
				number2 = 0;
				i--;
			}
        }
        if(number != 0){
        	sb.append(number);
        }
        System.out.println(sb);
        return calculateIIHelp(sb.toString());
    }
	
	//只有加减运算
	public int calculateIIHelp(String s) {
        if(s == null || s.length() <= 0){
        	return 0;
        }
        int length = s.length();
        int number = 0;
        int sign = 1;
        int result = 0;
        for(int i = 0; i < length; i++){
        	char c = s.charAt(i);
			if (Character.isDigit(c)) {
				number = 10 * number + (int) (c - '0');
			} else if (c == '+') {
				result += sign * number;
				number = 0;
				sign = 1;
			} else if (c == '-') {
				result += sign * number;
				number = 0;
				sign = -1;
			}
        }
        if (number != 0)
			result += sign * number;
        return result;
    }
}
